//
//  TSaleInfoDAL.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/11/7.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "TSaleInfoDAL.h"
#import "CSFMDBTool.h"

@implementation TSaleInfoDAL

#pragma mark - loadData
+ (TSaleInfoModel *)loadModelWithDict:(NSDictionary *)dict {
    //
    TSaleInfoModel * model = [[TSaleInfoModel alloc] init];
    model.sId = [dict[@"sId"] intValue];
    model.sOrderNo = dict[@"sOrderNo"];
    model.sCoNo = dict[@"sCoNo"];
    model.sName = dict[@"sName"];
    model.sCategory = dict[@"sCategory"];
    model.sUnit = dict[@"sUnit"];
    model.sPrice = dict[@"sPrice"];
    model.sCount = dict[@"sCount"];
    model.sUnitMoney = dict[@"sUnitMoney"];
    model.sTotalMoney = [dict[@"sTotalMoney"] floatValue];
    model.sWarehouseNo = dict[@"sWarehouseNo"];
    model.sWarehouseName = dict[@"sWarehouseName"];
    model.sSalesmanNo = dict[@"sSalesmanNo"];
    model.sSalesman = dict[@"sSalesman"];
    model.sBusinessmanNo = dict[@"sBusinessmanNo"];
    model.sBusinessman = dict[@"sBusinessman"];
    model.sSaleType = [dict[@"sSaleType"] intValue];
    model.sSettlementType = [dict[@"sSettlementType"] intValue];
    model.sPromotionType = [dict[@"sPromotionType"] intValue];
    model.sCreateTime = dict[@"sCreateTime"];
    model.sState = [dict[@"sState"] intValue];
    model.sRemark = dict[@"sRemark"];
    
    return model;
}

+ (NSArray<TSaleInfoModel *> *)loadListFromTSaleInfoWithUserNo:(NSString *)userNo businessNo:(NSString *)businessNo {
    
    NSString * sql = [NSString stringWithFormat:@"select * from tSaleInfo where sSalesmanNo='%@' and sBusinessmanNo='%@'; ",userNo, businessNo];
    return [self loadListWithSql:sql];
}

+ (NSArray<TSaleInfoModel *> *)loadListWithSql:(NSString *)sql {
    NSArray<NSDictionary *> * result = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!result) {
        return nil;
    }
    
    NSMutableArray<TSaleInfoModel *> *mArr = [NSMutableArray array];
    for (NSDictionary *dict in result)
    {
        TSaleInfoModel * model = [self loadModelWithDict:dict];
        
        [mArr addObject:model];
    }
    return mArr.copy;
}

+ (NSArray<TSaleInfoModel *> *)loadListWithOrderNo:(NSString *)orderNo {
    NSString * sql = [NSString stringWithFormat:@"select * from tSaleInfo where sOrderNo = '%@'; ", orderNo];
    
    return [self loadListWithSql:sql];
}

+ (NSArray<TSaleInfoModel *> *)loadListWithUserNo:(NSString *)userNo businessNo:(NSString *)businessNo daysBefore:(NSInteger)daysBefore {
    //
    NSDate * today = [[CSGXDateFormatterManager sharedManager] dateFromString:[[CSGXDateFormatterManager sharedManager] currentDay]];
    
    NSDate * before = [NSDate dateWithTimeInterval:-30*24*3600 sinceDate:today];
    
    
    NSString * sql = [NSString stringWithFormat:@"select * from tSaleInfo where sSalesmanNo='%@' and sBusinessmanNo='%@' and sCreateTime>'%@' order by sCreateTime desc; ",
                      userNo,
                      businessNo,
                      [[CSGXDateFormatterManager sharedManager] stringFromDate:before]
                      ];
    
//    NSLog(@"%@", sql);
    return [self loadListWithSql:sql];
}


#pragma mark - insert
+ (BOOL)insertIntoTSaleInfoWithArr:(NSArray<TSaleInfoModel *> *)saleModelList {
    NSMutableArray<NSNumber *> * insertIdsM = [NSMutableArray array];
    NSInteger lastId = 0;
    for (TSaleInfoModel *model in saleModelList) {
        lastId = [self insertIntoTSaleInfoWithModel:model];
        if (lastId == -1) {
            NSLog(@"插入失败，请检查！  ---model.sName: %@", model.sName);
            return NO;
        }
        [insertIdsM addObject:@(lastId)];
    }
    return YES;
}

+ (NSInteger)insertIntoTSaleInfoWithModel:(TSaleInfoModel *)saleModel {
    //
    NSString * sql = [NSString stringWithFormat:@"insert into tSaleInfo("
                      "sOrderNo, "
                      "sCoNo, "
                      "sName, "
                      "sCategory, "
                      "sUnit, "
                      "sPrice, "
                      "sCount, "
                      "sUnitMoney, "
                      "sTotalMoney, " //9
                      "sWarehouseNo, "
                      "sWarehouseName, "
                      "sSalesmanNo, "
                      "sSalesman, "
                      "sBusinessmanNo, "
                      "sBusinessman, "
                      "sSaleType, "
                      "sSettlementType, "
                      "sPromotionType, "
                      "sCreateTime, "
                      "sState, "
                      "sRemark"
                      ") "
                      "values('%@', '%@','%@', '%@', '%@', '%@','%@','%@', %.2f, '%@', '%@', '%@', '%@', '%@', '%@', %d, %d, %d,'%@', %d, '%@'); ",
                      saleModel.sOrderNo,
                      saleModel.sCoNo,
                      saleModel.sName,
                      saleModel.sCategory,
                      saleModel.sUnit,
                      saleModel.sPrice,
                      saleModel.sCount,
                      saleModel.sUnitMoney,
                      saleModel.sTotalMoney,
                      saleModel.sWarehouseNo,
                      saleModel.sWarehouseName,
                      saleModel.sSalesmanNo,
                      saleModel.sSalesman,
                      saleModel.sBusinessmanNo,
                      saleModel.sBusinessman,
                      saleModel.sSaleType,
                      saleModel.sSettlementType,
                      saleModel.sPromotionType,
                      saleModel.sCreateTime,
                      saleModel.sState,
                      saleModel.sRemark
                      ];
    
    return [[CSFMDBTool SharedFmdbTool] updateTableWithSql:sql];
}




#pragma mark - update

+ (void)updateSaleModelWithSql:(NSString *)sql {
    [[CSFMDBTool SharedFmdbTool] updateTableWithSql:sql];
}

+ (void)updateSaleModel:(TSaleInfoModel *)saleModel {
    if (!saleModel) {
        NSLog(@"Error: updateSaleModel");
        return;
    }
    
    NSString * sql = [NSString stringWithFormat:@"update tSaleInfo set "
                      "sOrderNo = '%@', "
                      "sCoNo = '%@', "
                      "sName = '%@', "
                      "sCategory = '%@', "
                      "sUnit = '%@', "
                      "sPrice = '%@', "
                      "sCount = '%@', "
                      "sUnitMoney = '%@', "
                      "sTotalMoney = %.2f, "
                      "sWarehouseNo = '%@', "
                      "sWarehouseName = '%@', "
                      "sSalesmanNo = '%@', "
                      "sSalesman = '%@', "
                      "sBusinessmanNo = '%@', "
                      "sBusinessman = '%@', "
                      "sSaleType = %d, "
                      "sSettlementType = %d, "
                      "sPromotionType = %d, "
                      "sCreateTime = '%@', "
                      "sState = %d, "
                      "sRemark = '%@' "
                      " where sId = %d; ",
                      
                      saleModel.sOrderNo,
                      saleModel.sCoNo,
                      saleModel.sName,
                      saleModel.sCategory,
                      saleModel.sUnit,
                      saleModel.sPrice,
                      saleModel.sCount,
                      saleModel.sUnitMoney,
                      saleModel.sTotalMoney,
                      saleModel.sWarehouseNo,
                      saleModel.sWarehouseName,
                      saleModel.sSalesmanNo,
                      saleModel.sSalesman,
                      saleModel.sBusinessmanNo,
                      saleModel.sBusinessman,
                      saleModel.sSaleType,
                      saleModel.sSettlementType,
                      saleModel.sPromotionType,
                      saleModel.sCreateTime,
                      saleModel.sState,
                      saleModel.sRemark,
                      (int)saleModel.sId
                      ];
//    NSLog(@"%@", sql);
    [self updateSaleModelWithSql:sql];
    
}

+ (void)updateSaleList:(NSArray<TSaleInfoModel *> *)saleList {
    for (TSaleInfoModel *model in saleList) {
        [self updateSaleModel:model];
    }
}









@end
